package com.example.controller.customer;

import com.example.dto.OrdersPaymentDTO;
import com.example.dto.OrdersRejectionDTO;
import com.example.dto.OrdersSubmitDTO;
import com.example.result.PageResult;
import com.example.result.Result;
import com.example.service.OrdersService;
import com.example.vo.OrderPaymentVO;
import com.example.vo.OrderSubmitVO;
import com.example.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 1. @ClassDescription:
 * 2. @author: Z
 * 3. @date: 2024年03月21日 23:04
 */
@RestController("customerOrderController")
@RequestMapping("/customer/order")
@Slf4j
@Api(tags = "C端-订单接口")
public class OrderController {
    @Autowired
    private OrdersService ordersService;

    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单:{}",ordersSubmitDTO);
        Result<OrderSubmitVO> result = ordersService.submitOrder(ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = result.getData();
        if(orderSubmitVO == null){
            return Result.error(result.getMsg());
        }
        return Result.success(orderSubmitVO);
    }


    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    @GetMapping("/historyOrders")
    @ApiOperation("历史数据订单")
    public Result<PageResult> historyOrders(int page, int pageSize, Integer status,Long storeId){

        PageResult pageResult = ordersService.historyOrders(page,pageSize,status,storeId);
        return Result.success(pageResult);
    }

    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> orderDetail(@PathVariable Long id){
        log.info("查询订单详情：{}",id);
        OrderVO orderVO = ordersService.orderDetail(id);
        return Result.success(orderVO);
    }

    @GetMapping("/orderDetailByOrderNumber/{orderNumber}")
    @ApiOperation("查询订单详情通过订单号")
    public Result<OrderVO> orderDetail(@PathVariable String orderNumber){
        log.info("查询订单详情通过订单号：{}",orderNumber);
        OrderVO orderVO = ordersService.orderDetailByOrderNumber(orderNumber);
        return Result.success(orderVO);
    }



    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancel(@PathVariable Long id){
        Result result = ordersService.userCancelById(id);
        return result;
    }


    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单") //再来一单就是将原订单中的商品重新加入到购物车中
    public Result repetition(@PathVariable Long id){
        ordersService.repetition(id);
        return Result.success();
    }

    @GetMapping("/reminder/{id}")
    @ApiOperation("催单")
    public Result reminder(@PathVariable Long id){
        Result result = ordersService.reminder(id);
        return result;
    }

    @PutMapping("/rejection")
    @ApiOperation("拒单")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO){
        log.info("拒单:{}",ordersRejectionDTO);
        ordersService.rejection(ordersRejectionDTO);
        return Result.success();
    }
}
